package com.yangaobo.mapper;

import com.yangaobo.dto.MenuPageDTO;
import com.yangaobo.entity.Menu;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 杨奥博
 */

@Repository
public interface MenuMapper {

    @Insert("""
            insert into ums_menu(`title`, `url`, `icon`, `pid`, `info`, `version`, `deleted`, `created`, `updated`)
            values (#{title}, #{url}, #{icon}, #{pid}, #{info}, #{version}, #{deleted}, #{created}, #{updated})
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Menu menu);

    @Results(
            id = "menuResultMap",
            value = {
                    @Result(property = "id", column = "id", id = true),
                    @Result(property = "pid",column = "pid"),
                    @Result(property = "menu",column = "pid",one = @One(select = "com.yangaobo.mapper.MenuMapper.select"))
            }
    )
    @Select("""
            select * from ums_menu t
            where t.id = #{param} and t.deleted = 0
            """)
    Menu select(Long id);

    @ResultMap("menuResultMap")
    @Select("""
            <script>
            select * from ums_menu t
            <where>
                <if test='title != null'> title like concat('%', #{title}, '%') and </if>
                <if test='pid != null'> pid = #{pid} and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<Menu> list(MenuPageDTO dto);

    @Update("""
            <script>
            update ums_menu
            <set>
                <if test='title != null'> title = #{title}, </if>
                <if test='url != null'> url = #{url}, </if>
                <if test='icon != null'> icon = #{icon}, </if>
                <if test='pid != null'> pid = #{pid}, </if>
                <if test='info != null'> info = #{info}, </if>
                <if test='deleted != null'> deleted = #{deleted}, </if>
                <if test='created != null'> created = #{created}, </if>
                <if test='updated != null'> updated = #{updated}, </if>
                version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """)
    int update(Menu menu);

    @Update("""
            update ums_menu set deleted = 1, updated = current_timestamp where id = #{id}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update ums_menu set deleted = 1, updated = current_timestamp where
            <foreach collection='list' item='e' open='id in (' close= ')' separator=','>
                #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

    @Select("""
            select * from ums_menu t1 where
            id in (
                select t2.fk_menu_id from ums_role_menu t2
                where t2.fk_role_id = #{roleId} and t2.deleted = 0
            ) and t1.deleted = 0
            """)
    List<Menu> listByRoleId(Long roleId);

    @Update("""
            update ums_role_menu set deleted = 1, updated = current_timestamp where fk_role_id = #{roleId}
            """)
    int deleteByRoleId(Long roleId);

    @Insert("""
            <script>
            insert into ums_role_menu(fk_role_id, fk_menu_id, version, deleted, created, updated) values 
            <foreach collection='ids' item='e' separator=','>
            (#{roleId}, #{e}, 0, 0, current_timestamp, current_timestamp)
            </foreach>
            </script>
            """)
    int updateByRoleId(@Param("roleId") Long roleId, @Param("ids") List<Long> ids);

    @ResultMap("menuResultMap")
    @Select("""
            select * from ums_menu t1 where 
            id in (
                select t2.fk_menu_id from ums_role_menu t2
                where t2.fk_role_id in (
                    select t3.fk_role_id from ums_emp_role t3
                    where t3.fk_emp_id = #{param1} and t3.deleted = 0
                ) and t2.deleted = 0
            ) and t1.deleted = 0
            """)
    List<Menu> listByEmpId(Long empId);

}
